﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Quartz;
using TicketClient.Common;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Enums;
using YouWei.Infrastructure.Service;
using YouWei.Utility;
using YouWei.Utility.Logging;

namespace ServiceContainer.Jobs
{


    /// <summary>
    /// 短信发送状态更新作业
    /// </summary>
    [DisallowConcurrentExecution]
    public class SmsStateUpdateJob : IJob
    {


        
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                while (true)
                {
                    DoWork();
                    Thread.Sleep(60000);
                    DoExtraWork();
                }
            }
            catch (Exception ex)
            {
                Log4netLog.Error(101,context.JobDetail.Key+"作业出现异常,异常信息为："+ex);
            }
        }

        /// <summary>
        /// 从mysql获取状态更新到mssql, 推送形式
        /// </summary>
        private void DoWork()
        {
            SmsMessagePoolService svMsgPool = new SmsMessagePoolService();
            sms_sendService svMsgSend = new sms_sendService();
            //var listMsg = svMsgPool.GetNeedToUpdateStateMsgs();
            //foreach (var msg in listMsg)
            //{
            //    var oSmsSend = svMsgSend.SingleOrDefault(msg.SmsCatSendID);
            //    var nSendState = EConvert.ToInt(oSmsSend.smsState);
            //    if (nSendState != msg.SendStatus)
            //    {
            //        msg.SendStatus = nSendState;
            //        msg.SendStatusUpdatedTime = DateTime.Now;
            //        msg.SendMobile = oSmsSend.PhoNum;
            //        bool bUpdate = svMsgPool.Update(msg);
            //        if (!bUpdate)
            //        {
            //            Log4netLog.Error("更新短信发送状态时失败，失败数据为：" + msg.ToJSON());
            //        }
            //    }
            //}
            //换成mysql推送mssql的形式，解决堵塞的问题
            SyncConfig.Instance.Load();
            int lastUpdateID = SyncConfig.Instance.LastUpdateID;
            int groupCount = 100;
            var listMsg = svMsgSend.GetNeedToUpdateStateMsgs(lastUpdateID,groupCount);
            
            for (var i=0;i<listMsg.Count;i++)
            {
                var msg = listMsg[i];
                var oMsg = svMsgPool.QuerySingle(m=>m.SmsCatSendID==msg.Id);
                if (oMsg == null)
                {
                    continue;
                }
                var nSendState = EConvert.ToInt(msg.smsState);
                if (nSendState != oMsg.SendStatus)
                {
                    oMsg.SendStatus = nSendState;
                    oMsg.SendStatusUpdatedTime = DateTime.Now;
                    oMsg.SendMobile = msg.PhoNum;
                    bool bUpdate = svMsgPool.Update(oMsg);
                    if (!bUpdate)
                    {
                        Log4netLog.Error("更新短信发送状态时失败，失败数据为：" + msg.ToJSON());
                    }
                }
                SyncConfig.Instance.LastUpdateID = msg.Id;
                SyncConfig.Instance.Save();

            }

           


        }

        /// <summary>
        /// 从mssql数据拉取数据，根据mysql状态更新， 拉取形式
        /// </summary>
        private void DoExtraWork()
        {
            SmsMessagePoolService svMsgPool = new SmsMessagePoolService();
            sms_sendService svMsgSend = new sms_sendService();
            var listMsg = svMsgPool.GetNeedToUpdateStateMsgs();
            foreach (var msg in listMsg)
            {
                var oSmsSend = svMsgSend.SingleOrDefault(msg.SmsCatSendID);
                var nSendState = EConvert.ToInt(oSmsSend.smsState);
                if (nSendState != msg.SendStatus)
                {
                    msg.SendStatus = nSendState;
                    msg.SendStatusUpdatedTime = DateTime.Now;
                    msg.SendMobile = oSmsSend.PhoNum;
                    bool bUpdate = svMsgPool.Update(msg);
                    if (!bUpdate)
                    {
                        Log4netLog.Error("更新短信发送状态时失败，失败数据为：" + msg.ToJSON());
                    }
                }
            }

        }


    }
}
